﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using LapvComponent;

namespace Lapv
{
    public partial class Default : System.Web.UI.Page
    {
        static int length = 100;
     public  int userid = 0;
        public string domain = ConfigurationManager.AppSettings["DomainNews"];
    public string type = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["userid"] != null)
            {
                userid = int.Parse(Request.Params["userid"]);
            }
            if (Request.Params["tid"] != null)
            {
                type = Request.Params["tid"].ToString();
            }

            if (Page.IsPostBack) return;
           
            if (Page.User.Identity.IsAuthenticated == false)
                Response.Redirect(domain + "Login.aspx?returnurl=" + Request.RawUrl.ToString());
            loaduser(userid);
            length = 100;
            bindData(userid);
        }
        public string GetType(string type)
        {
            string reval = string.Empty;
            switch (type)
            {
                case "USER_ADMIN_ADD":
                    reval = "Thêm thành viên quản trị";
                    break;
                case "USER_ADMIN_EDIT":
                    reval = "Sửa thông tin thành viên quản trị";
                    break;
                case "USER_Lapv_DELETE":
                    reval = "Xóa account quản trị";
                    break;
                case "NEWS_ADD":
                    reval = "Thêm tin tức";
                    break;
                case "NEWS_EDIT":
                    reval = "Sửa tin tức";
                    break;
                case "NEWS_DELETE":
                    reval = "Xóa tin tức";
                    break;
                case "NEWSCATE_ADD":
                    reval = "Thêm danh mục tin";
                    break;
                case "NEWSCATE_EDIT":
                    reval = "Sửa danh mục tin";
                    break;
                case "NEWSCATE_DELETE":
                    reval = "Xóa danh mục tin";
                    break;
                case "SURVEY_ADD":
                    reval = "Thêm survey";
                    break;
                case "SURVEY_EDIT":
                    reval = "Sửa survey";
                    break;
                case "SURVEY_DELTE":
                    reval = "Xóa survey";
                    break;
                case "PERMISSION_ADD":
                    reval = "Thêm Permission";
                    break;
                case "PERMISSION_EDIT":
                    reval = "Sửa Permission";
                    break;
                case "PERMISSION_DELETE":
                    reval = "Xóa Permission";
                    break;
                case "USER_ADMIN_SET_IP":
                    reval = "Đổi IP tài khoản";
                    break;
                case "USER_ADMIN_CHANGE_PASS":
                    reval="Đổi mật khẩu tài khoản";
                    break;
                case "ROLE_DELETE":
                    reval = "Xóa role";
                    break;
                case "ROLE_ADD":
                    reval = "Thêm role";
                    break;
                case "ROLE_EDIT":
                    reval = "Sửa role";
                    break;
                    
                default:
                    break;
            }
            return reval;
 
        }
        private void bindData(int userid)
        {
            List<log> lst = null;
            using (lapvEntities db = new lapvEntities())
            {
                if (length > 0)
                {
                    if (userid > 0)
                    {
                        lst = db.logs.Where(en => en.gmId == userid).OrderByDescending(en => en.date).Take(length).ToList();
                    }
                    else
                    {
                        lst = db.logs.OrderByDescending(en => en.date).Take(length).ToList();
                    }
                    if (type != "") {
                        lst = lst.Where(en => en.type == type).ToList();
                    }
                }
                else
                {
                    if (userid > 0)
                    {
                        lst = db.logs.Where(en => en.gmId == userid).OrderByDescending(en => en.date).ToList();
                    }
                    else
                    {
                        lst = db.logs.OrderByDescending(en => en.date).ToList();
                    }
                }

                totalcount.Text = lst.Count.ToString();
            }
            if (lst != null && lst.Count > 0)
            {
                dtgrid.DataSource = lst;
                dtgrid.DataBind();
            }
            else
            {
                dtgrid.DataSource = null;
                dtgrid.DataBind();
            }
        }
        void loaduser(int userid)
        {
            using (lapvEntities db = new lapvEntities())
            {
                List<account> lst = db.accounts.ToList();
                ddlUser.DataSource = lst;
                ddlUser.DataTextField = "username";
                ddlUser.DataValueField = "id";
                ddlUser.DataBind();
            }
            if (userid > 0)
                ddlUser.SelectedValue = userid.ToString();
        }
        protected void btnTop100_Click(object sender, EventArgs e)
        {
            if (Request.Params["userid"] != null)
            {
                userid = int.Parse(Request.Params["userid"]);
            }
            length = 100;
            bindData(userid);
        }
        protected void btnTop1000_Click(object sender, EventArgs e)
        {
            if (Request.Params["userid"] != null)
            {
                userid = int.Parse(Request.Params["userid"]);
            }
            length = 1000;
            bindData(userid);
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Request.Params["userid"] != null)
            {
                userid = int.Parse(Request.Params["userid"]);
            }
            length = 0;
            bindData(userid);
        }

        protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            userid = int.Parse(ddlUser.SelectedValue);
            Response.Redirect(domain + "default.aspx?userid=" + userid);            
        }
    }
}
